package com.lzy.shops.controller.backstage;

import com.fasterxml.jackson.core.JsonProcessingException;
import com.fasterxml.jackson.databind.ObjectMapper;
import com.lzy.shops.bean.Area;
import com.lzy.shops.service.AreaService;
import com.lzy.shops.utils.ResponseData;
import com.lzy.shops.utils.exception.ActionException;
import com.lzy.shops.utils.exception.ExistException;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.redis.core.StringRedisTemplate;
import org.springframework.data.redis.core.ValueOperations;
import org.springframework.web.bind.annotation.*;

import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

@RestController
@RequestMapping("/system/area")
public class AreaController {

    @Autowired
    private AreaService areaService;

    @Autowired
    private StringRedisTemplate redisTemplate;

    ObjectMapper objectMapper = new ObjectMapper();

    @GetMapping("/getProvince")
    public String getProvince() throws JsonProcessingException {
        Map<String, Object> map = new HashMap<>();
        Area province = new Area();
        ValueOperations valueOperations = redisTemplate.opsForValue();
        String area = (String) valueOperations.get("area");
        if(area == null||area.equals("")){
            try {
                province = areaService.findProvince(0);
                if(province!=null){
                    map.put("data", province);
                }else{
                    return objectMapper.writeValueAsString(new ResponseData(false,"错误"));
                }
            } catch (Exception e) {
                return objectMapper.writeValueAsString(new ResponseData(false,"错误"));
            }
            String address = objectMapper.writeValueAsString(new ResponseData<Map<String, Object>>(true, map, "省市区"));
            valueOperations.set("area",address);
            return address;
        }else{
            return area;
        }
    }

    @PostMapping("/addArea")
    public String addArea(@RequestBody Area area) throws JsonProcessingException {
        Integer integer = 0;
        try {
            integer = areaService.addArea(area);
            if(integer!=null && integer!=0){
                redisTemplate.delete("area");
                return objectMapper.writeValueAsString(new ResponseData<Area>(true,"添加成功"));
            }
        } catch (ExistException e) {
            return objectMapper.writeValueAsString(new ResponseData<Area>(false,e.getMessage()));
        }
        return objectMapper.writeValueAsString(new ResponseData<Area>(false,"错误"));
    }

    @DeleteMapping("/delete/{area_id}")
    public String delArea(@PathVariable("area_id") Integer id) throws JsonProcessingException {
        Map<String, String> info = new HashMap<>();
        try {
            info  = areaService.deleteArea(id);
            redisTemplate.delete("area");
            return objectMapper.writeValueAsString(new ResponseData<Map>(true,info));
        } catch (ActionException e) {
            return objectMapper.writeValueAsString(new ResponseData<Map>(false,"操作失败"));
        }
    }

    @GetMapping("/findByName")
    public String findByName(@RequestParam("area_name") String name) throws JsonProcessingException {
        List<Area> byName = areaService.findByName(name);
        return objectMapper.writeValueAsString(new ResponseData<List<Area>>(true,byName));
    }

    @GetMapping("/getTree/{id}")
    public Map<String, Object> getTree(@PathVariable("id") int id) {
        Map<String, Object> map = new HashMap<>();
        try {
            Area province = areaService.findProvince(id); //查询出一个省
            if (province != null) {
                List<Area> citys = areaService.findChildren(province.getId());//查询省下面的所有市
                digui(citys); //调用递归算法查询市以下的区县
                province.setChildren(citys);
            }
            map.put("data", province);
        } catch (Exception e) {
            e.printStackTrace();
        }
        return map;
    }

    public void digui(List<Area> citys) {
        List<Area> retList = new ArrayList<>();
        for (Area c : citys) {
            retList = areaService.findChildren(c.getId());
            if (retList.size() > 0) {
                c.setIsLeaf(true);
                c.setChildren(retList);
                if(retList.size()>=1){
                    digui(retList);
                }
            }
        }
    }
}
